﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace NhanDang
{
    class DataProvider
    {
        public static bool CheckFileExists(string fileName)
        {
            if (File.Exists(Application.StartupPath +"//Data//" + fileName+".txt"))
                return true;
            return false;
        }

        public static void OverWriteArrayToFile(string fileName, double[,] arrData, int numRow, int numCol)
        {
            if (!CheckFileExists(fileName))
                File.Create(Application.StartupPath + "//Data//" + fileName + ".txt").Close();
            StreamWriter write = File.CreateText(Application.StartupPath + "//Data//" + fileName + ".txt");
            for (int i = 0; i < numRow; i++)
            {
                for (int j = 0; j < numCol; j++)
                {
                    write.WriteLine(arrData[i, j]);
                }
            }
            write.Flush();
            write.Close();
        }
        public static void WriteArrayToFile(string fileName, double[,] arrData, int numRow, int numCol)
        {
            StreamWriter write = File.AppendText(Application.StartupPath + "//Data//" + fileName + ".txt");
            for (int i = 0; i < numRow; i++)
            {
                for (int j = 0; j < numCol; j++)
                {
                    write.WriteLine(arrData[i, j]);
                }
            }
            write.Flush();
            write.Close();
        }
        public static void WriteArrayToFile(string fileName, double[] arrData)
        {
            StreamWriter write = File.AppendText(Application.StartupPath + "//Data//" + fileName + ".txt");
            for (int i = 0; i < arrData.Length; i++)
            {
                write.WriteLine(arrData[i]);
            }
            write.Flush();
            write.Close();
        }

        public static double[][,] ReadData(string fileName, int numInput, int numOut, int numHidden)
        {
            double[][,] data = new double[3][,];
            data[0] = new double[numInput,numHidden];
            data[1] = new double[numHidden,numOut];
            data[2] = new double[1,numOut];
            FileStream fileStream = new FileStream(Application.StartupPath + "//Data//" + fileName + ".txt", FileMode.Open);
            StreamReader streamReader = new StreamReader(fileStream);
            for (int i = 0; i < numInput; i++)
            {
                for (int j = 0; j < numHidden; j++)
                {
                    data[0][i, j] = double.Parse(streamReader.ReadLine());
                }
            }
            for (int i = 0; i < numHidden; i++)
            {
                for (int j = 0; j < numOut; j++)
                {
                    data[1][i,j] = double.Parse(streamReader.ReadLine());
                }
            }
            for (int i = 0; i < numOut; i++)
            {
                data[2][0,i] = double.Parse(streamReader.ReadLine());
            }
            streamReader.Close();
            fileStream.Close();
            return data;
        }

        public static double[] desiredOutput(int i, int output)
        {
            double[] re = new double[output];
            re[i] = 1;
            return re;
        }
        public static string Name(string s)
        {
            int k = -1;
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] == '1')
                {
                    k = i;
                    break;
                }
            }
            switch (k)
            {
                case 0: return "Hình chữ nhật";
                case 1: return "Hình sao bốn cánh";
                case 2: return "Hình sao năm cánh";
                case 3: return "Hình sao sáu cánh";
                case 4: return "Hình tam giác";
                case 5: return "Hình tròn";
                case 6: return "Hình tứ giác";
                default:
                    return "";
            }
        }
    }
}
